package gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import common.Constants;
import common.EarthSimSettings;

public class QueryOutputSettingsPanel extends JPanel implements ActionListener {
  private static final long serialVersionUID = 8788164723977195424L;

  private EarthSimSettings settings = EarthSimSettings.getInstance();
  private JCheckBox chkMinTemp = new JCheckBox(Constants.MIN_TEMP);
  private JCheckBox chkMaxTemp = new JCheckBox(Constants.MAX_TEMP);
  private JCheckBox chkMeanTempRegion = new JCheckBox(Constants.MEAN_TEMP_REGION);
  private JCheckBox chkMeanTempTime = new JCheckBox(Constants.MEAN_TEMP_TIME);
  private JCheckBox chkCellValues = new JCheckBox(Constants.CELL_VALUES);
  
  public QueryOutputSettingsPanel() {
    super();    
    setLayout(new GridLayout(0,2));
    setBorder(BorderFactory.createTitledBorder("Output Settings"));
    setAlignmentX(Component.LEFT_ALIGNMENT);
    
    Dimension dim = new Dimension(390,90);
    setPreferredSize(dim);
    setMaximumSize(dim);
    
    addListeners();
    add(chkMinTemp);
    add(chkMaxTemp);
    add(chkMeanTempRegion);
    add(chkMeanTempTime);
    add(chkCellValues);
  }
  
  private void addListeners() {
    chkMinTemp.setActionCommand(Constants.MIN_TEMP);
    chkMinTemp.addActionListener(this);
    
    chkMaxTemp.setActionCommand(Constants.MAX_TEMP);
    chkMaxTemp.addActionListener(this);
    
    chkMeanTempRegion.setActionCommand(Constants.MEAN_TEMP_REGION);
    chkMeanTempRegion.addActionListener(this);
    
    chkMeanTempTime.setActionCommand(Constants.MEAN_TEMP_TIME);
    chkMeanTempTime.addActionListener(this);
    
    chkCellValues.setActionCommand(Constants.CELL_VALUES);
    chkCellValues.addActionListener(this);
  }
  
  public void actionPerformed(ActionEvent e) {
    if(e.getActionCommand().equals(Constants.MIN_TEMP)) {
      settings.setShowingMinTemp(chkMinTemp.isSelected());
    }
    else if(e.getActionCommand().equals(Constants.MAX_TEMP)) {
      settings.setShowingMaxTemp(chkMaxTemp.isSelected());
    }
    else if(e.getActionCommand().equals(Constants.MEAN_TEMP_REGION)) {
      settings.setShowingMeanTempRegion(chkMeanTempRegion.isSelected());
    }
    else if(e.getActionCommand().equals(Constants.MEAN_TEMP_TIME)) {
      settings.setShowingMeanTempTime(chkMeanTempTime.isSelected());
    }
    else if(e.getActionCommand().equals(Constants.CELL_VALUES)) {
      settings.setShowingCellValues(chkCellValues.isSelected());
    }
  }

}
